using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/**树 */
public class Trunk : MonoBehaviour
{
    /**移动速度 */
    public float speed = 5.0f;
    
    /**子弹飞行速度 */
    public float bullletSpeed = 10.0f;

    /**子弹预制体 */
    public GameObject bulletPrefab;
    
    /**左位置对象 */
    public Transform leftObj;
    
    /**右位置对象 */
    public Transform rightObj;
    
    /**左子弹位置对象 */
    public Transform leftBulletObj;
    
    /**右子弹位置对象 */
    public Transform rightBulletObj;
    
    private BoxCollider2D _boxCollider2D;
    private BoxCollider2D _triggerCollider2D;
    private Animator _animator;
    
    /**初始位置 */
    private Vector3 _originalPosition;
    
    /**左坐标x */
    private float _leftX;
    
    /**右坐标x */
    private float _rightX;
    
    /**左子弹坐标x */
    private float _leftBulletX;
    
    /**右子弹坐标x */
    private float _rightBulletX;
    
    /**是否攻击 */
    private bool _isAttack = false;
    
    /**是否跑动中 */
    private bool _isRun = false;
    
    private float _timer = 0.0f;

    private static readonly int Attack = Animator.StringToHash("attack");
    private static readonly int Idle = Animator.StringToHash("idle");
    private static readonly int Run = Animator.StringToHash("run");

    // Start is called before the first frame update
    void Start()
    {
        _boxCollider2D = GetComponents<BoxCollider2D>()[0];
        _triggerCollider2D = GetComponents<BoxCollider2D>()[1];
        _animator = GetComponent<Animator>();
        
        _originalPosition = transform.position;

        _leftX = leftObj.position.x;
        _rightX = rightObj.position.x;
        
        _leftBulletX = leftBulletObj.position.x;
        _rightBulletX = rightBulletObj.position.x;
    }

    // Update is called once per frame
    void Update()
    {
        if (_isAttack)
        {
            _timer = 0.0f;
            return;
        }
        
        if (!_isRun)
        {
            _timer += Time.deltaTime;
            if (_timer > 0.5f)
            {
                _isRun = true;
                _animator.SetTrigger(Run);
            }
            return;
        }
        _timer = 0.0f;

        var position = transform.position;
        position = new Vector3(position.x + speed * Time.deltaTime, position.y, position.z);
        
        if (position.x < _leftX)
        {
            transform.position = new Vector3(_leftX, position.y, position.z);
            speed = Math.Abs(speed);
            
            var scale = transform.localScale;
            transform.localScale = new Vector3(-Math.Abs(scale.x), scale.y, scale.z);
        } 
        else if (position.x > _rightX)
        {
            transform.position = new Vector3(_rightX, position.y, position.z);
            speed = Math.Abs(speed) * -1;
            
            var scale = transform.localScale;
            transform.localScale = new Vector3(Math.Abs(scale.x), scale.y, scale.z);
        }
        else
        {
            transform.position = new Vector3(position.x, position.y, position.z);
        }
    }
    
    private void OnTriggerStay2D(Collider2D col)
    {
        if (col.gameObject.CompareTag("Player") && !_isAttack)
        {
            _isAttack = true;
            _animator.SetTrigger(Attack);

            var scale = transform.localScale;
            speed = col.transform.position.x < transform.position.x ? -Math.Abs(speed) : Math.Abs(speed);
            transform.localScale = new Vector3(col.transform.position.x < transform.position.x ? Math.Abs(scale.x) : -Math.Abs(scale.x), scale.y, scale.z);
            
            Invoke(nameof(FireBullet), 0.56f);
            Invoke(nameof(AttackFinish), 0.8f);
        }
    }

    /**发射子弹 */
    private void FireBullet()
    {
        var position = transform.position;
        var bulletData = new BulletData();
        bulletData.speed = transform.localScale.x > 0 ? -bullletSpeed : bullletSpeed;
        bulletData.endPos = new Vector3(transform.localScale.x > 0 ? _leftBulletX : _rightBulletX, position.y, position.z);
        
        BulletMgr.instance.FireBullet(bulletPrefab, position, bulletData);
    }
    
    /**攻击完成 */
    private void AttackFinish()
    {
        _isAttack = false;
        _animator.SetTrigger(Idle);
    }
}
